package com.lemon.smartsafety.ui.adapter.safe.data;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.DataInfo;
import com.lemon.smartsafety.entity.safe.company.FloorNumItem;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.utils.common.StringUtils;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by Cooliean on 16/3/27.
 */
public class BuildingFloorAdapter extends LemonRcvAdapter<FloorNumItem> {
    private RecyclerTClickListener<FloorNumItem> mListener;
    private boolean isSafeInspection = false;

    public BuildingFloorAdapter(@Nullable List<FloorNumItem> data, RecyclerTClickListener<FloorNumItem> listener) {
        super(data);
        mListener = listener;
        isSafeInspection = false;
    }

    public BuildingFloorAdapter(@Nullable List<FloorNumItem> data, RecyclerTClickListener<FloorNumItem> listener, boolean inspection) {
        super(data);
        mListener = listener;
        this.isSafeInspection = inspection;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new BuildingFloorItem(mListener);
    }


    public class BuildingFloorItem implements AdapterItem<FloorNumItem> {


        private RecyclerTClickListener<FloorNumItem> mListener;
        LinearLayout      ripple;
        CircleImageView imCircle;
        TextView        tvTitle;

        FloorNumItem mData;
        int          mPosition;

        public BuildingFloorItem(RecyclerTClickListener<FloorNumItem> mListener) {
            this.mListener = mListener;
        }

        @Override
        public int getLayoutResId() {
            return R.layout.item_floor_view;
        }

        @Override
        public void bindViews(View root) {
            ripple = (LinearLayout) root.findViewById(R.id.ripple);

            imCircle = (CircleImageView) root.findViewById(R.id.im_circle);
            tvTitle = (TextView) root.findViewById(R.id.tv_title);
        }

        @Override
        public void setViews() {
            ripple.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onItemClick(mData, mPosition);
                    }
                }
            });

        }

        @Override
        public void handleData(FloorNumItem floorNumItem, int position) {
            mPosition = position;
            mData = floorNumItem;
            tvTitle.setText(StringUtils.nullValue(floorNumItem.getItemTitle()));
            if (!isSafeInspection) {//一般的数据采集
                if (floorNumItem.isCurrent()) {
                    //imCircle.setBackgroundColor(tvTitle.getContext().getResources().getColor(R.color.colorAppBlue));
                    imCircle.setBackgroundResource(R.drawable.base_blue_click_pressed);
                    tvTitle.setTextColor(tvTitle.getContext().getResources().getColor(R.color.colorWhite));
                } else {
                    //imCircle.setBackgroundColor(tvTitle.getContext().getResources().getColor(R.color.colorWhite));
                    imCircle.setBackgroundResource(R.drawable.base_click_pressed);
                    tvTitle.setTextColor(tvTitle.getContext().getResources().getColor(R.color.colorTextPrimary));
                }
            } else {//安全巡查的颜色控制
                if (floorNumItem.isCurrent()) {
                    //imCircle.setBackgroundColor(tvTitle.getContext().getResources().getColor(R.color.colorAppBlue));
                    imCircle.setBackgroundResource(R.drawable.base_blue_click_pressed);
                    tvTitle.setTextColor(tvTitle.getContext().getResources().getColor(R.color.colorWhite));
                } else {
                    String statusString = StringUtils.nullValue(floorNumItem.getStatus(), "0");
                    if (DataInfo.RED_TAG.equals(statusString)) {
                        //imCircle.setBackgroundColor(tvTitle.getContext().getResources().getColor(R.color.red));
                        imCircle.setBackgroundResource(R.drawable.base_red_click_pressed);
                        tvTitle.setTextColor(tvTitle.getContext().getResources().getColor(R.color.colorWhite));
                    } else if (DataInfo.YELLOW_TAG.equals(statusString)) {
                       // imCircle.setBackgroundColor(tvTitle.getContext().getResources().getColor(R.color.yellow));
                        imCircle.setBackgroundResource(R.drawable.base_yellow_click_pressed);
                        tvTitle.setTextColor(tvTitle.getContext().getResources().getColor(R.color.colorWhite));
                    } else if (DataInfo.GREEN_TAG.equals(statusString)) {
                        //imCircle.setBackgroundColor(tvTitle.getContext().getResources().getColor(R.color.green));
                        imCircle.setBackgroundResource(R.drawable.base_green_click_pressed);
                        tvTitle.setTextColor(tvTitle.getContext().getResources().getColor(R.color.colorWhite));
                    } else {
                        //imCircle.setBackgroundColor(tvTitle.getContext().getResources().getColor(R.color.white));
                        imCircle.setBackgroundResource(R.drawable.base_click_pressed);
                        tvTitle.setTextColor(tvTitle.getContext().getResources().getColor(R.color.colorTextPrimary));
                    }
                }
            }
        }
    }
}
